package cn.edu.swu.ws.controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession(false);
        String username = "未知用户";

        if (session != null) {
            username = (String) session.getAttribute("user");
            session.invalidate(); // 销毁session
            System.out.println("✅ 用户 " + username + " 已退出登录");
        }

        // 重定向到登录页面
        response.sendRedirect(request.getContextPath() + "/login.html");
    }
}